<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Models\Company\Archive;
use App\Models\Company\BackstageCategory;
use App\Models\Company\Category;
use App\Models\Company\Material;
use App\Models\Encyclopedia\Article;
use App\Models\System\Region;
use Illuminate\Support\Collection;

class CompanyController extends Controller
{
    public function index()
    {
        $city = is_domain_top() ? '' : config('region.region.model', '');
        $model = Archive::with('region')
            ->when($city == '', function ($query) {
                return $query->where('is_top', true);
            })
            ->where('is_state', true)
            ->orderBy('id', SORT_DESC);
        $archive = $model->where(function ($query) use ($city) {
            if ($city) {
                $query->where('region_id', $city->id);
            }
        })->paginate(15);
        $city = config('region.region.model', '');

        if (request()->get('clear', '')) {
            \Cache::clear();
        }
        if (\Cache::offsetExists($city->id ?? 0)) {
            $cache = \Cache::get($city->id ?? 0);
        } else {
            $top = $model->where('region_id', $city->id ?? 46)->inRandomOrder()->take(3)->get();
            $recommend = $model->where('region_id', $city->id ?? 46)->inRandomOrder()->take(10)->get();
            $cache = \Cache::remember($city->id ?? 0, 1440, function () use ($top, $recommend) {
                return ['top' => $top, 'recommend' => $recommend];
            });
        }
        $top = $cache['top'];
        $recommend = $cache['recommend'];
        $article = Article::take(13)->get();
        $article = $article->map(function (Article $item) {
            $item->media->path = \Storage::url($item->media->path);
            return $item;
        });
        $regions = Region::all();
        return view('region.company', compact('archive', 'top', 'recommend', 'article', 'regions'));
    }

    public function detail($id)
    {
        $archive = Archive::where('is_state', true)->with('region')->find($id);
        $title = null;
        if (strpos($archive->name, '装修') !== false) {
            $title = '装修';
            $article = Article::where('title', 'like', '%装修%')->inRandomOrder()->take(6)->get();
        } elseif (strpos($archive->name, '装饰') !== false) {
            $title = '装饰';
            $article = Article::where('title', 'like', '%装饰%')->inRandomOrder()->take(6)->get();
        } elseif (strpos($archive->name, '装潢') !== false) {
            $title = '装潢';
            $article = Article::where('title', 'like', '%装潢%')->inRandomOrder()->take(6)->get();
        } elseif (strpos($archive->name, '装璜') !== false) {
            $title = '装璜';
            $article = Article::where('title', 'like', '%装璜%')->inRandomOrder()->take(6)->get();
        } else {
            $article = Article::inRandomOrder()->take(6)->get();
        }

        $sheji = Article::where('title', 'like', '%设计%')->inRandomOrder()->take(6)->get();

        $sheji = $sheji->map(function (Article $item) {
            $item->media->path = \Storage::url($item->media->path);
            return $item;
        });

        $randArchive = Archive::where('is_state', true)
            ->where('name', 'like', '%' . $title . '%')
            ->where('region_id', $archive->region_id)->take(12)->get();

        $article = $article->map(function (Article $item) {
            $item->media->path = \Storage::url($item->media->path);
            return $item;
        });
        return view('region.detail', compact('archive', 'article', 'randArchive', 'sheji'));
    }

    public function list()
    {
        $url = request()->url();
        if (request()->get('clear', '')) {
            \Cache::clear();
        }
        if (\Cache::offsetExists($url)) {
            $categorys = \Cache::get($url);
        } else {
            $categorys = BackstageCategory::get();
            $categorys->each(function (BackstageCategory $backstageCategory) {
                $backstageCategory->load('takeMaterial.region');
            });
            $categorys = \Cache::remember($url, 1440, function () use ($categorys) {
                return $categorys;
            });
        }
        $article = Article::inRandomOrder()->with('media')->take(12)->get();
        $article = $article->map(function (Article $item) {
            $item->media->path = \Storage::url($item->media->path);
            return $item;
        });
        return view('region.list', compact('categorys', 'article'));
    }

    public function material($uri = null)
    {
        $category = Category::where('uri', $uri)->first();
        if (!$category) {
            abort(404);
        }
        $city = config('region.region.model', '');
        $model = Material::with('region')->where('is_state', true)
            ->where('category_id', $category->id)
            ->where(function ($query) use ($city) {
                if ($city && !is_domain_top()) {
                    $query->where('region_id', $city->id);
                }else{
                    $query->where('is_top', true);
                }
            });
        $materials = $model->orderBy('updated_at', 'desc')->paginate(15);
        if (request()->get('clear', '')) {
            \Cache::clear();
        }
        if (\Cache::offsetExists($uri)) {
            $cache = \Cache::get($uri);
        } else {
            $material_rand = $model->inRandomOrder()->take(6)->get();
            $article = Article::inRandomOrder()->with('media')->take(12)->get();
            $article = $article->map(function (Article $item) {
                $item->media->path = \Storage::url($item->media->path);
                return $item;
            });
            $cache = \Cache::remember($uri, 1440, function () use ($material_rand, $article) {
                return ['material_rand' => $material_rand, 'article' => $article];
            });
        }
        $material_rand = $cache['material_rand'];
        $article = $cache['article'];
        return view('region.material', compact('materials', 'material_rand', 'article', 'category'));
    }

    public function materialDetail($id)
    {
        $key = request()->url();
        if (\Cache::offsetExists($key)) {
            $cache = \Cache::get($key);
        } else {
            $material = Material::where('is_state', true)->with('region')->find($id);
            $title = null;
            if ($material && strpos($material->name, '装修') !== false) {
                $title = '装修';
                $article = Article::where('title', 'like', '%装修%')->inRandomOrder()->take(6)->get();
            } elseif ($material && strpos($material->name, '装饰') !== false) {
                $title = '装饰';
                $article = Article::where('title', 'like', '%装饰%')->inRandomOrder()->take(6)->get();
            } elseif ($material && strpos($material->name, '装潢') !== false) {
                $title = '装潢';
                $article = Article::where('title', 'like', '%装潢%')->inRandomOrder()->take(6)->get();
            } elseif ($material && strpos($material->name, '装璜') !== false) {
                $title = '装璜';
                $article = Article::where('title', 'like', '%装璜%')->inRandomOrder()->take(6)->get();
            } else {
                $article = Article::inRandomOrder()->take(6)->get();
            }

            $sheji = Article::where('title', 'like', '%设计%')->inRandomOrder()->take(6)->get();

            $sheji = $sheji->map(function (Article $item) {
                $item->media->path = \Storage::url($item->media->path);
                return $item;
            });

            $randMaterials = Material::where('is_state', true)
                ->where('name', 'like', '%' . $title . '%')
                ->where('region_id', $material->region_id ?? 0)->take(12)->get();

            $article = $article->map(function (Article $item) {
                $item->media->path = \Storage::url($item->media->path);
                return $item;
            });
            $data = [
                'material' => $material,
                'article' => $article,
                'sheji' => $sheji,
                'randMaterials' => $randMaterials,
            ];
            $cache = $this->cache($data, $key);
        }
        $material = $cache['material'];
        if (!$material){
            abort(404);
        }
        $article = $cache['article'];
        $randMaterials = $cache['randMaterials'];
        $sheji = $cache['sheji'];
        return view('region.material_detail', compact('material', 'article', 'randMaterials', 'sheji'));
    }

    public function cache($data = [], $key)
    {
        $cache = \Cache::remember($key, 1440, function () use ($data) {
            return $data;
        });
        return $cache;
    }
}